package com.dai.spec.web.core.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.dai.spec.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
 
@Slf4j
@Component
public class AutoFillMetaInfoHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("====>start insert fill.....");
       //如果数据库字段类型时date,将LocalDateTime.now()改为new Date()
        this.setFieldValByName("createTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("createUser", SecurityUtils.getLoginUser().getUsername(),metaObject);
    }
 
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("====>start update fill.....");
        this.setFieldValByName( "updateUser",SecurityUtils.getLoginUser().getUsername(),metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
    }
}